package biz;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import dao.TOrderJew;
import dao.TOrderJewAccs;
import dao.TOrderJewDetail;
import dao.TOrderJewGold;
import dao.TOrderJewStone;
import dao.TOrderJewVstone;
import dao.VwOrderJew;

public class JewOrderBiz {
	Configuration configuration = null;
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;
	
	public void AddOrderJew(TOrderJew sarv){
		ModOrderJew(sarv);
	}
	
	public void ModOrderJew(TOrderJew sarv){
		String maxid = "";
		String qtype = "Z";
		DbComBiz dbbiz = new DbComBiz();
		if(null == sarv.getId() || "".equals(sarv.getId().trim())){
			maxid = dbbiz.getMaxId(qtype);
			sarv.setId(qtype+maxid);
		}
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOrderJew(TOrderJew sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOrderJew tddmp = (TOrderJew)session.get(TOrderJew.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOrderJew(){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("SELECT t1.id,t3.name AS maker,t1.subtime,t1.status,t2.name AS clientname,t1.clientex,t1.metalper,t1.goldprice,t1.charprint,t1.prepay,t1.outdate,t4.name AS receiver,t1.note FROM TOrderJew AS t1,TClientInfo AS t2,TUserInfo AS t3,TUserInfo AS t4 WHERE t1.clientid = t2.id AND t1.makerid = t3.id AND t1.receiverid = t4.id ORDER BY t1.id ASC ");
		
		List list = new ArrayList();
		Iterator iter =query.iterate();
		while(iter.hasNext()){
			Object[] obj = (Object[])iter.next();
			VwOrderJew voj = new VwOrderJew(obj[0].toString(),obj[1].toString(),obj[2].toString().substring(0,18),obj[3].toString(),obj[4].toString(),obj[5].toString(),obj[6].toString(),obj[7].toString(),obj[8].toString(),obj[9].toString(),obj[10].toString().substring(0,7),obj[11].toString(),obj[12].toString());
			list.add(voj);
		}
		
		session.close();
		return list;
	}
	
	public Object GetOrderJew(String oid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery(" FROM TOrderJew as f where f.id='" + oid + "' order by f.id ");
		
		List list = query.list();
		
		session.close();
		if(list != null) {
			return list.get(0);
		}else{
			return null;
		}
		
	}
	
	public void AddOrderJewAccs(TOrderJewAccs sarv){
		ModOrderJewAccs(sarv);
	}
	
	public void ModOrderJewAccs(TOrderJewAccs sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOrderJewAccs(TOrderJewAccs sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOrderJewAccs tddmp = (TOrderJewAccs)session.get(TOrderJewAccs.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOrderJewAccs(String orderid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("SELECT t1.id,t3.`name` AS maker,t1.subtime,t1.`status`,t2.`name` AS clientname,t1.clientex,t1.metalper,t1.goldprice," +
		"t1.charprint,t1.prepay,t1.outdate,t4.`name` AS receiver,t1.note FROM t_order_jew AS t1,t_client_info AS t2,t_user_info AS t3,t_user_info AS t4 " +
		"WHERE t1.clientid = t2.id AND t1.makerid = t3.id AND t1.receiverid = t4.id ORDER BY t1.id ASC");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public void AddOrderJewDetail(TOrderJewDetail sarv){
		ModOrderJewDetail(sarv);
	}
	
	public void ModOrderJewDetail(TOrderJewDetail sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOrderJewDetail(TOrderJewDetail sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOrderJewDetail tddmp = (TOrderJewDetail)session.get(TOrderJewDetail.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOrderJewDetail(String orderid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TOrderJewDetail as f where f.jeworderid='" + orderid + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public void AddOrderJewGold(TOrderJewGold sarv){
		ModOrderJewGold(sarv);
	}
	
	public void ModOrderJewGold(TOrderJewGold sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOrderJewGold(TOrderJewGold sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOrderJewGold tddmp = (TOrderJewGold)session.get(TOrderJewGold.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOrderJewGold(String orderid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TOrderJewGold as f where f.jeworderid='" + orderid + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public void AddOrderJewStone(TOrderJewStone sarv){
		ModOrderJewStone(sarv);
	}
	
	public void ModOrderJewStone(TOrderJewStone sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOrderJewStone(TOrderJewStone sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOrderJewStone tddmp = (TOrderJewStone)session.get(TOrderJewStone.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOrderJewStone(String orderid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TOrderJewStone as f where f.jeworderid='" + orderid + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public void AddOrderJewVstone(TOrderJewVstone sarv){
		ModOrderJewVstone(sarv);
	}
	
	public void ModOrderJewVstone(TOrderJewVstone sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOrderJewVstone(TOrderJewVstone sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOrderJewVstone tddmp = (TOrderJewVstone)session.get(TOrderJewVstone.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOrderJewVstone(String orderid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TOrderJewVstone as f where f.jeworderid='" + orderid + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		JewOrderBiz jj = new JewOrderBiz();
		jj.GetOrderJew();
		

	}

}
